let obj = { a: 1, b: 2 }
console.log(obj)
let p = new Proxy(obj, {
    //get拦截器
    get: function (target, prop) {
        if (prop in target) {
            return target[prop]
        }
        else {
            return ''
        }
    },
    //set拦截器
    set: function (target, prop, value) {
        //只允许设置值为数字
        if (typeof (value) === 'number') {
            target[prop] = value
        }
        else {
            throw new Error('不能设置为非数值')
        }
    }
})
//读取值
console.log(p.a)
console.log(p.c)

//设置值
try {
    p.a = 'hello'
}
catch (e) {
    console.log(e.message)
}

//代理器的操作，也会改变目标对象的值
// p.a = 18
// console.log(obj)

//构造器代理
let handler2 = {
    construct:function(target,args,newTarget){
        console.log('进入到构造器代理方法')
        console.log(target,[...args],newTarget)
    }
}
let People = class {
    constructor(name,age){
        this.name = name
        this.age = age 
    }
}

People = new Proxy(People,handler2)
console.log(new People('zhansan',18))
// let people = new proxy('张三',18)

let User = class {
    constructor(name) {
        this.name = name
    }
}
User = new Proxy(User, {
    construct(target, args, newTarget) {
        console.log('construct')
        console.log(newTarget)
        return new target(...args)
    }
})
console.log(new User('imooc'))

